package com.hangim.pce.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import com.hangim.pce.model.SysOrgDrug;
import com.hangim.pce.service.SysOrgDrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 机构药品关系表 前端控制器
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@RestController
@RequestMapping("/sysOrgDrug")
public class SysOrgDrugController extends BaseController {
    @Autowired
    private SysOrgDrugService sysOrgDrugService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysOrgDrug
     */
    @GetMapping("/{id}")
    public R<SysOrgDrug> get(@PathVariable Long id) {
        return new R<>(sysOrgDrugService.selectById(id));
    }


    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysOrgDrugService.selectPage(new Query<>(params));
    }

    /**
     * 添加
     *
     * @param sysOrgDrug 实体
     * @return success/false
     */
    @PostMapping
    public R<Integer> add(@RequestBody SysOrgDrug sysOrgDrug) {
        return new R<>(sysOrgDrugService.insert(sysOrgDrug));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Integer> delete(@PathVariable Long id) {
        SysOrgDrug sysOrgDrug = new SysOrgDrug();
        sysOrgDrug.setId(id);
        sysOrgDrug.setUpdatedAt(new Date());
        sysOrgDrug.setActive(0);
        return new R<>(sysOrgDrugService.updateById(sysOrgDrug));
    }

    /**
     * 编辑
     *
     * @param sysOrgDrug 实体
     * @return success/false
     */
    @PutMapping
    public R<Integer> edit(@RequestBody SysOrgDrug sysOrgDrug) {
        sysOrgDrug.setUpdatedAt(new Date());
        return new R<>(sysOrgDrugService.updateById(sysOrgDrug));
    }
}
